
var feed;
var map;
var markers;
var infoWindows;
var lat;
var lng;

lat = new Array();
lng = new Array();
lat[0] = 1.2727
lat[1] = 1.2749
lat[2] = 1.2720
lat[3] = 1.2745
lat[4] = 1.2707

lng[0] = 103.8073
lng[1] = 103.8003
lng[2] = 103.7983
lng[3] = 103.8043
lng[4] = 103.8033

var names = new Array();
names[0] = "Big Burger"
names[1] = "Pizza Place"
names[2] = "Xiao Long Bao"
names[3] = "Chinese Cuisine"
names[4] = "Indian Food"


var blocked;

function RestaurantInfoDemo(){   
    
    blocked = false;
    
//    feed = SAPFeed("http://10.33.247.66:8000", this, 31, "aaa");
    feed = SAPFeed("http://10.33.247.153:8000", this, 31, "aaa");
    
    this.onSAPFeedLogin = function(){
        var datasetList = [];
        datasetList.push(13);
        feed.listen(datasetList); //Listen to specific datasets
        
//        showMessage("success", "Login Success", "Welcome to the Demo");
    }
    this.onSAPFeedLogout = function(){
        showMessage("success", "Logout Success", "Thank you for using our service");
    }
    
    this.onSAPFeedError = function(responseReceived){
        showMessage("error", "Error: "+responseReceived.code, responseReceived.message);
        if(responseReceived.code=="E999"){
            if(!blocked){
                blocked = true;
                $('#map_canvas').fadeTo(1000, 0, function(){
                    alert("You have unsubscribed from the data stream, application will resume when you re-subscribe to it");
                });
            }
        }        
    }

    this.onSAPFeedListen = function(){
        showMessage("success", "Listen Success", "Now you are able to receive information from your channel");
        loadMapScript();
    }
    
    this.onSAPFeedReceive = function(publisherID, datasetID, data){
        
        showMessage("info", "Information Received", "You have received information ");
        
        if(blocked){
            blocked = false;
                $('#map_canvas').fadeTo(1000, 1);            
        }
        
        
        
        var classModifier;
        if(data.message=="Available"){
            classModifier = "green_bold";
            markers[data.id].setAnimation(google.maps.Animation.BOUNCE);

        } else if(data.message=="Normal"){
            classModifier = "coral_bold";
            markers[data.id].setAnimation(null);
        } else if(data.message=="Crowded"){
            classModifier = "red_bold";
            markers[data.id].setAnimation(null);
        }
        
        infoWindows[data.id].setContent("<div class='info_div'><h4 class='info_title'>"+data.name+"</h4>"+"<p class='info_content'>Average waiting time: <span class='time_special'>"+data.time+"min</span></p>"+"<p class='info_content "+classModifier+"'>Status: "+data.message+"</p></div>");
        infoWindows[data.id].open(map, markers[data.id]);
    }

}

$(document).ready(function(){
    
    hideAllMessages();
    RestaurantInfoDemo();
    $('#stop_btn').click(function(){
        feed.logout();
    });
});



function initialize() {
    var myOptions = {           
        center: new google.maps.LatLng(1.2742, 103.8023),           
        zoom: 17,        
        mapTypeId: google.maps.MapTypeId.ROADMAP         
    };
    map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
    initializeMarkers();
}

function loadMapScript() {
    var script = document.createElement("script");   
    script.type = "text/javascript";   
    script.src = "http://maps.googleapis.com/maps/api/js?sensor=true&callback=initialize";
    document.body.appendChild(script); 
} 
        
function initializeMarkers(){
    
    markers = new Array();
    infoWindows = new Array();    
    
    var i;
    for(i=0;i<5;i++){
        markers[i] = new google.maps.Marker({
            map: map,
            draggable: false,
            position: new google.maps.LatLng(lat[i], lng[i])
        });
        
        infoWindows[i] = new google.maps.InfoWindow({
            disableAutoPan: true
            
        });
        
        infoWindows[i].setContent("<div class='info_div'><h4 class='info_title'>"+names[i]+"</h4>"+"<p class='info_content'>Average waiting time: <span class='time_special'>"+6+"min</span></p>"+"<p class='info_content coral_bold'>Status: "+"Normal"+"</p></div>");
        infoWindows[i].open(map, markers[i]);        
        
    }

   
}